<template>
  <div class="projectAdd" v-loading="detailInfoShow">
    <!-- 筛选按钮 -->
    <div class="search-button" v-if="!contactAddShow && !saleAddShow">
      <el-button class="btn direction" @click="handleBack"
      ><el-icon class="icon" color="#fff"> <Back /> </el-icon>返回</el-button
      >
    </div>
    <div class="content">
     <div>
      <el-image
                  :preview-teleported="true"
                  style="width: 200px; height: 100px; margin: 0 auto;margin-right:30px;"
                  :src="rowdata.img"
                >
                  <!-- :preview-src-list="showImg(row.materialPicture)" -->
                  <template #error>
                    <div class="image-slot">
                      <el-icon class="icon" :size="30" color="#909399"> <Picture /> </el-icon>
                    </div>
                  </template>
                </el-image>
     </div>
     <div>
      <div class="content-top">
        <div class="content-title">{{ rowdata.materialName }}</div>
        <div class="content-tab">
          <div class="item-tab" v-if="rowdata.needTestName">
            {{ rowdata.needTestName }}
          </div>
        </div>
      </div>
      <div class="content-bottom">
        <div class="item">
          <div class="title">物料编号：</div>
          <div class="text">{{ rowdata.materialCode }}</div>
        </div>
        <div class="item">
          <div class="title">物料分类：</div>
          <div class="text">{{ rowdata.categoryFullName }}</div>
        </div>
      </div>
     </div>
    </div>
    <el-tabs type="border-card" v-model="activeName" @tab-change="changetab">
      <el-tab-pane label="基础信息" name="info">
        <el-collapse v-model="activeNames">
          <!-- 基础信息 -->
          <el-collapse-item name="1" class="transform-down">
            <template #title>
              <span class="circle"></span>
              <span class="collapse-title">基础信息</span>
            </template>

            <div>
              <el-descriptions
                  border
                  style="margin: 20px 0"
                  column="2"
                  label-align="center"
                  align="center"
              >
                <el-descriptions-item
                    label="采购名称"
                    label-class-name="desLabel"
                    class-name="desInfo"
                    label-align="center"
                    align="center"
                >{{
                    rowdata.purchaseName || "--"
                  }}</el-descriptions-item
                >
                <el-descriptions-item
                    label="销售名称"
                    label-class-name="desLabel"
                    class-name="desInfo"
                    label-align="center"
                    align="center"
                >{{
                    rowdata.saleName || "--"
                  }}</el-descriptions-item
                >
                <el-descriptions-item
                    label="出厂型号"
                    label-class-name="desLabel"
                    class-name="desInfo"
                    label-align="center"
                    align="center"
                >{{
                    rowdata.factoryCode || "--"
                  }}</el-descriptions-item
                >
                <el-descriptions-item
                    label="销售型号"
                    label-class-name="desLabel"
                    class-name="desInfo"
                    label-align="center"
                    align="center"
                >{{
                    rowdata.saleCode || "--"
                  }}</el-descriptions-item
                >
                <el-descriptions-item
                    label="技术参数"
                    label-class-name="desLabel"
                    class-name="desInfo"
                    label-align="center"
                    align="center"
                >{{
                    rowdata.technologyParameter || "--"
                  }}</el-descriptions-item
                >
                <el-descriptions-item
                    label="数量单位"
                    label-class-name="desLabel"
                    class-name="desInfo"
                    label-align="center"
                    align="center"
                >{{
                    rowdata.materialUnitName || "--"
                  }}</el-descriptions-item
                >
                <el-descriptions-item
                    label="预警数量"
                    label-class-name="desLabel"
                    class-name="desInfo"
                    label-align="center"
                    align="center"
                >{{
                    rowdata.warningNumber || "--"
                  }}</el-descriptions-item
                >
                <el-descriptions-item
                    label="是否允许负库存"
                    label-class-name="desLabel"
                    class-name="desInfo"
                    label-align="center"
                    align="center"
                >
                    <div>{{ rowdata.underStock == 0 ? '否' : '是' }}</div>
                </el-descriptions-item>

                <el-descriptions-item
                    label="供应商"
                    label-class-name="desLabel"
                    label-align="center"
                    align="center"
                >{{ rowdata.supplierName || "--" }}</el-descriptions-item
                >
                <el-descriptions-item
                    label="生产厂家"
                    label-class-name="desLabel"
                    label-align="center"
                    align="center"
                >{{ rowdata.manufactureName || "--" }}</el-descriptions-item
                >
                <el-descriptions-item
                    label="默认税率"
                    label-class-name="desLabel"
                    label-align="center"
                    align="center"
                >{{ rowdata.defaultTaxRateName || "--" }}</el-descriptions-item
                >
                <el-descriptions-item
                    label="首次采购日期"
                    label-class-name="desLabel"
                    label-align="center"
                    align="center"
                >{{
                    rowdata.createTime || "--"
                  }}</el-descriptions-item
                >
              </el-descriptions>
            </div>
          </el-collapse-item>
        </el-collapse>
      </el-tab-pane>
      <el-tab-pane label="产品评估" name="approval">
        <el-collapse v-model="activeNames">
          <!-- 基础信息 -->
          <el-collapse-item name="1">
            <template #title>
              <span class="circle"></span>
              <span class="collapse-title">产品测评</span>
            </template>
          </el-collapse-item>
        </el-collapse>

        <el-tabs v-model="activeNameStatus" class="demo-tabs" @tab-click="handleClick">
          <el-tab-pane label="已测评" name="已测评">
            <!-- 表格数据 -->
            <el-table
                :data="projectList"
                border
                height="100%"
                :header-cell-style="{ 'text-align': 'center' }"
            >
              <el-table-column
                  label="操作"
                  :index="indexMethod"
                  width="60"
                  align="center"
              >
                <template #default="{ row }">
                  <div class="table-operate">
                    <el-icon
                        title="查看"
                        color="#007CEF"
                        class="table-icon"
                        :size="20"
                        @click="handlePersonTable(row)"
                    >
                      <Document />
                    </el-icon>

                  </div>

                </template>
              </el-table-column>
              <el-table-column
                  type="index"
                  label="序号"
                  :index="indexMethod"
                  width="80"
                  align="center"
              />
              <el-table-column
                  label="开始时间"
                  prop="actualStartTime"
                  :show-overflow-tooltip="true"
              />
              <el-table-column
                  label="结束时间"
                  prop="finishTime"
                  :show-overflow-tooltip="true"
              />
              <el-table-column
                  label="情况说明"
                  prop="performance"
                  :show-overflow-tooltip="true"
              />
              <el-table-column
                  label="相关文档"
                  prop="address"
                  width="260"
                  :show-overflow-tooltip="true"
              >
                <template #default="{ row }">
                  <span
                      v-for="(item, index) in row.fileList"
                      :key="index"
                      @click="handleFileClick(item)"
                      class="fileText"
                      style="cursor: pointer"
                  >
                    {{ item.name }}
                    <span
                      style="color: #000"
                      v-if="index + 1 !== row.fileList.length"
                  >，</span
                  ></span
                  >
                </template>
              </el-table-column>
              <el-table-column
                  label="责任人"
                  prop="deptName"
                  :show-overflow-tooltip="true"
              >
                <template #default="{ row }">
              <span v-for="item in row.userList" style="margin-right: 5px;">
                {{ item.nickName }}
              </span>
                </template>
              </el-table-column>
              <el-table-column
                  label="提交时间"
                  prop="createTime"
                  :show-overflow-tooltip="true"
              />
            </el-table>

            <!-- 分页 -->
            <pagination
                class="pagination"
                :total="projectTotal"
                v-model:page="params.pageNum"
                v-model:limit="params.pageSize"
                @pagination="getTaskListTemp"
            />
          </el-tab-pane>
          <el-tab-pane label="待测评" name="待测评">
            <el-table
                :data="projectNoList"
                border
                height="100%"
                :header-cell-style="{ 'text-align': 'center' }"
            >
              <el-table-column
                  label="操作"
                  :index="indexMethod"
                  width="80"
                  align="center"
              >
                <template #default="{ row }">
                  <el-icon
                      title="查看"
                      color="#007CEF"
                      class="table-icon"
                      :size="20"
                      @click="handlePersonTable(row)"
                  >
                    <Document />
                  </el-icon>
                </template>
              </el-table-column>
              <el-table-column
                  type="index"
                  label="序号"
                  :index="indexMethod"
                  width="80"
                  align="center"
              />
              <el-table-column
                  label="计划开始时间"
                  prop="createTime"
                  :show-overflow-tooltip="true"
              />
              <el-table-column
                  label="计划结束时间"
                  prop="endTime"
                  :show-overflow-tooltip="true"
              />
              <el-table-column
                  label="任务内容"
                  prop="remark"
                  :show-overflow-tooltip="true"
              />

              <el-table-column
                  label="责任人"
                  prop="deptName"
                  :show-overflow-tooltip="true"
              >
                <template #default="{ row }">
              <span v-for="item in row.userList" style="margin-right: 5px;">
                {{ item.nickName }}
              </span>
                </template>
              </el-table-column>
              <el-table-column
                  label="创建人"
                  prop="createUserName"
                  :show-overflow-tooltip="true"
              />
              <el-table-column
                  label="创建时间"
                  prop="createTime"
                  :show-overflow-tooltip="true"
              />
            </el-table>
            <!-- 分页 -->
            <pagination
                class="pagination"
                :total="total"
                v-model:page="construction.pageNum"
                v-model:limit="construction.pageSize"
                @pagination="getNoTaskListTemp"
            />

          </el-tab-pane>
        </el-tabs>
      </el-tab-pane>
    </el-tabs>

    <!--  工单详情  -->
    <ProjectDetail
        v-if="workOrderDetailShow"
        ref="projectCheckRef"
        v-model:projectAddShow="workOrderDetailShow"
        @updateFollowAddShow="followAddShow = false"
    />
  </div>
</template>

<script setup>
import ProjectDetail from "@/views/operation/taskProject/projectDetail.vue";
import { getInfoApi,getTaskList,getNoTaskList } from "@/api/operation/productEstimate.js"
import { ElMessage, ElMessageBox } from "element-plus";
import useUserStore from "@/store/modules/user";
const userStore = useUserStore();
const route = useRoute();
const emit = defineEmits([
  // 项目新增组件显隐
  "contractDtlShow",
  // 更新项目新增组件显隐
  "update:contractDtlShow",
  // 刷新父组件表格数据
  "refreshList",
  // 更新跟进组件显隐
  "updateFollowAddShow",
]);
const props = defineProps({
  // 操作类型
  pageType: {
    type: String,
    default: "add",
  },
});
// 搜索参数
const params = reactive({
  pageNum: 1,
  pageSize: 50,
});
// 总数
const projectTotal = ref(0);
const total = ref(0);
const construction = reactive({
  pageNum: 1,
  pageSize: 50,
});

const statusList = ref(['已测评', '待测评']);
const activeNameStatus = ref('已测评')
const activeName = ref("info");
// 折叠栏默认展开
const activeNames = ref(["1", "2", "3", "4", "5"]);
// 工单详情弹窗
const workOrderDetailShow = ref(false);
const projectCheckRef = ref();

// 详情加载效果
const detailInfoShow = ref(false);

// 输入框引用
const refInput = ref();

// 评论
const evaluateDialogShow = ref(false);
// 评论内容
const evaluateInfo = ref("");
const title = ref("");



let auditList = ref([]);
// 新增联系人表单显隐
const contactAddShow = ref(false);
// 新增销售列表表单显隐
const saleAddShow = ref(false);
const rowdata = ref({});
//提示的显隐
const showtooltip = ref(false);

//自定义数字限制
const digitalRestrictions = (rule, value, callback) => {
  if (value < 0 || value > 9999) {
    callback(new Error("请输入0-9999之内的数字"));
  } else {
    callback();
  }
};
const projectList = ref([]);
const projectNoList = ref([]);

import print from "vue3-print-nb";
import {Base64} from "js-base64";

// 使用自定义指令
const vPrint = print;
const printFlag = ref(false);

let prints = {
  id: "print-iframe",
  popTitle: "配置页眉标题", // 打印配置页上方的标题
  extraHead: "", // 最上方的头部文字，附加在head标签上的额外标签，使用逗号分割
  preview: false, // 是否启动预览模式，默认是false
  previewTitle: "预览的标题", // 打印预览的标题
  previewPrintBtnLabel: "预览结束，开始打印", // 打印预览的标题下方的按钮文本，点击可进入打印
  zIndex: 20002, // 预览窗口的z-index，默认是20002，最好比默认值更高
  previewBeforeOpenCallback() {
    console.log("正在加载预览窗口！");
  }, // 预览窗口打开之前的callback
  previewOpenCallback() {
    console.log("已经加载完预览窗口，预览打开了！");
  }, // 预览窗口打开时的callback
  // 开始打印之前的callback
  beforeOpenCallback() {
    console.log("开始打印之前！");
  },
  openCallback() {
    console.log("执行打印了！");
  }, // 调用打印时的callback
  closeCallback() {
    console.log("关闭了打印工具！");
    printFlag.value = false;
  }, // 关闭打印的callback(无法区分确认or取消)
  clickMounted() {
    console.log("点击v-print绑定的按钮了！");
  },
  standard: "",
  extarCss: "",
};


/**
 * @description: 处理文件点击
 * @param {Object} item 文件信息
 */
const handleFileClick = (item) => {
  let docUrl = item.url;
  let officeUrl =
      "http://101.71.158.60:50098/onlinePreview?url=" +
      encodeURIComponent(Base64.encode(docUrl)) +
      "&watermarkTxt=" +
      encodeURIComponent(userStore.nickName); // 在新窗口打开编码后的链接
  window.open(officeUrl, "_target");
};

/**
 * @description: 序号
 * @param {Number} index 索引
 */
function indexMethod(index) {
  const currentPage = params.pageNum;
  const pageSize = params.pageSize;
  const truelndex = (currentPage - 1) * pageSize + index + 1;
  return truelndex;
}


// 关闭评论弹窗
const closeEvaluate = () => {
  evaluateDialogShow.value = false;
  evaluateInfo.value = "";
};


/**
 * @description: 返回
 */
const handleBack = () => {
  emit("updateFollowAddShow", false);
};


/**
 * @description: 处理详情信息
 * @param {Number} id
 */
const handleDetailInfo = async (row) => {
  await getInfoApi({ materialId: row } ).then(res => {
    if (res) {
      rowdata.value = res.material;
      // projectList.value = res.taskList;
      if (res.materialFileList.length > 0) rowdata.value.img =  res.materialFileList && res.materialFileList[0].url
    }
  })
  await getTaskListTemp(row);
  await getNoTaskListTemp(row);
};


const getTaskListTemp = (id) => {
  let param = {
    materialId: id
  };
  let { pageNum, pageSize } = params;
  getTaskList({ pageNum, pageSize }, param).then((res) => {
    if (res.code === 200) {
      projectList.value = res.rows;
      projectTotal.value = res.total;
    }
  })
};

const getNoTaskListTemp = (id) => {
  let param = {
    materialId: id
  };
  let { pageNum, pageSize } = construction;
  getNoTaskList({ pageNum, pageSize }, param).then((res) => {
    if (res.code === 200) {
      projectNoList.value = res.rows;
      total.value = res.total;
    }
  })
};


/*
* 跳转到任务中心弹窗
* */
const handlePersonTable = (row) => {
  workOrderDetailShow.value = true;
  nextTick(() => {
    projectCheckRef.value.handleDetailInfo(row.missionCenterId,{businessTypeName:'产品评估',subtaskTypeName:'产品评估'});
  })
}




defineExpose({
  handleDetailInfo,
});
</script>
<style lang="scss" scoped>
:deep(.el-collapse-item__header) {
  background-color: #f8faff;
}
.projectAdd {
  padding: 20px 20px 0 20px;
  width: 100%;
  height: 100%;
  :deep(.el-date-editor.el-input, .el-date-editor.el-input__wrapper) {
    width: 268px !important;
    height: 34px !important;
  }
  :deep(.pagination-container) {
    height: 56px;
  }
  .content {
    display:flex;
    width: 1604px;
    /* height: 130px; */
    background: #ffffff;
    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.1);
    padding: 20px;
    // margin-top:10px;
    margin-bottom: 20px;
    .content-top {
      display: flex;
      align-items: center;
      .content-title {
        font-family: PingFang SC;
        font-weight: 500;
        font-size: 18px;
        color: #333333;
        line-height: 42px;
      }
      .content-tab {
        display: flex;
        align-items: center;
        .item-tab {
          padding: 3px 5px;
          margin: 0 10px;
        }
        .tooltip {
          display: flex;
          align-items: center;
          .triangle {
            width: 0;
            height: 0;
            border-left: 10px solid transparent;
            border-right: 10px solid transparent;
            border-bottom: 17.3px solid #fdefdc; /* 边长为100px时，高度为边长的正弦值乘以2/3 */
            transform: rotate(-90deg);
          }
          .contenttext {
            max-width: 800px;
            padding: 10px;
            background-color: #fdefdc;
            transform: translateX(-5px);
            color: #d95700;
          }
        }
        .item-tab:nth-child(1) {
          color: rgba(87, 107, 249, 1);
          background-color: rgba(87, 107, 249, 0.2);
        }
        .item-tab:nth-child(2) {
          color: rgba(230, 122, 37, 1);
          background-color: rgba(230, 122, 37, 0.2);
        }
        .item-tab:nth-child(3) {
          color: rgba(63, 161, 122, 1);
          background-color: rgba(63, 161, 122, 0.2);
        }
      }
    }
    .content-bottom {
      display: flex;
      margin: 20px 0;
      font-family: PingFang SC;
      font-weight: 400;
      font-size: 14px;
      color: #333333;
      .item {
        width: 200px;
        display: flex;
        align-items: center;
      }
    }
  }
  .project-info {
    padding: 20px 20px 5px 20px;
    background-color: #f0f0f0;
    margin: 10px 0;
  }
  .contact-info {
    padding-top: 20px;
  }
  .project-title {
    display: flex;
    padding-top: 30px;
  }
  .schedule-form {
    margin: 30px 0;
    .form {
      display: flex;
      margin-top: 15px;
      background-color: #fafbff;
      .form-left {
        width: 20%;
        height: 100%;
        .item {
          background-color: #d9dce4;
          height: 50px;
          line-height: 50px;
          text-align: center;
          border: 1px solid #f2f2f2;
          cursor: pointer;
        }
        .item:first-child {
          background-color: unset;
          font-size: 18px;
          font-weight: 700;
        }
        .activeitem {
          background-color: #007cef;
        }
      }
      .form-right {
        width: 85%;
        padding-left: 20px;
        border: 1px solid #f2f2f2;
        padding-right: 20px;

        .right-title {
          height: 50px;
          line-height: 50px;
          font-size: 18px;
          font-weight: 700;
        }
        .subtitle {
          display: flex;
          align-items: center;
          margin: 12px 0;
          .dot {
            width: 13px;
            height: 13px;
            background-color: #0179ed;
            border-radius: 50%;
            margin-right: 10px;
          }
        }
        .right-form {
          /* height: calc(100% - 50px); */
          display: flex;
          flex-direction: column;
          justify-content: space-between;
        }
      }
    }
  }
  :deep(.el-table) {
    th.el-table__cell {
      background: #f8f8f8 !important;
    }
  }
  .ription {
    color: #ff0000;
    line-height: 16px;
    font-size: 12px;
  }
  :deep(.el-step__head.is-success) {
    color: #3399ff;
    border-color: #3399ff;
    position: relative;
  }
  :deep(.el-step__title) {
    font-size: 16px;
    line-height: 38px;
    position: absolute;
    top: -38px;
    width: 140px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  :deep(.el-step__title.is-success) {
    color: #3399ff;
  }
  :deep(.desLabel) {
    background: transparent !important;
    width: 200px;
    font-weight: 500 !important;
  }
  .desContent {
    // width: 100%;
    width: 330px;
    overflow: hidden;
    white-space: nowrap; /* 确保文本在一行内显示 */
    text-overflow: ellipsis; /* 使用省略号表示文字被截断 */
  }
  :deep(.desInfo) {
    width: 200px;
  }
  .desBox {
    height: 50px;
    display: flex;
    align-items: center
  }
}
.dialog-title {
  background-color: #fdefdc;
  width: 100%;
  height: 40px;
  display: flex;
  align-items: center;
  padding-left: 20px;
  margin-bottom: 20px;
}
.el-table {
  border: var(--el-table-border);
}
.image-slot {
  width:100%;
  height:100%;
  background-color:#F5F7FA;
  display:flex;
  justify-content:center;
  align-items:center;
}
</style>

<style>
.dialogBox .el-dialog__body {
  padding: 10px 20px !important;
}
</style>
